package com.sunmouse.toolkit.fragment.chart;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.sunmouse.toolkit.fragment.BaseFragment;

import java.util.ArrayList;

import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ComboLineColumnChartView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ComboFragment extends BaseFragment {


    private ComboLineColumnChartView comboChart;

    public ComboFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        comboChart = new ComboLineColumnChartView(getActivity());
        return comboChart;
    }

    @Override
    public void init() {
        LineChartData lineData = generateLineData();
        ColumnChartData columnData = generateColumnData();
        ComboLineColumnChartData data = generateData(lineData, columnData);
        this.comboChart.setComboLineColumnChartData(data);
    }

    private ComboLineColumnChartData generateData(LineChartData lineData, ColumnChartData columnData) {
        ComboLineColumnChartData data = new ComboLineColumnChartData(columnData, lineData);
        data.setAxisXBottom(generateXAxis());
        data.setAxisYLeft(generateYAxis());
        return data;
    }

    private ColumnChartData generateColumnData() {
        ArrayList<Column> columns = new ArrayList<>();
        ArrayList<SubcolumnValue> subColumns;
        for (int i = 0; i < 7; i++) {
            subColumns = new ArrayList<>();
            subColumns.add(new SubcolumnValue((float) (50 * Math.random()), getCircleColor()));
            columns.add(new Column(subColumns));
        }
        ColumnChartData columnData = new ColumnChartData(columns);
        return columnData;
    }


    private LineChartData generateLineData() {
        ArrayList<Line> lines = new ArrayList<>();
        ArrayList<PointValue> points = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            points.add(new PointValue(i, 1 + (int) (10 * Math.random())));
        }
        lines.add(new Line(points));
        LineChartData data = new LineChartData(lines);

        return data;
    }

}
